<%@page import="java.text.SimpleDateFormat"%>
<%@page import="weaver.conn.RecordSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.zlgs.cqyf.vote.ConnectionUtil"%>
<%@ page import="com.alibaba.fastjson.*" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.util.Date" %>
<%
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json; charset=utf-8");
    JSONObject retObject = new JSONObject();
    retObject.put("code", 200);
    retObject.put("msg", "操作成功");
    try {
        // URL 参数
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer dateType = Integer.parseInt(request.getParameter("dateType"));
        JSONArray array = new JSONArray();
        Date date = new Date();
        if(dateType != null ){
            // 数据库查询
            String sql = "select * from vote_awards where id in (" +
                    "SELECT a.awards_id FROM vote_awards_subname a " +
                    "LEFT JOIN vote_awards b ON a.awards_id= b.id " +
                    "WHERE b.is_del = 0 AND b.date_type=" +dateType.intValue()+
                    " AND a.is_del= 0 AND a.status IN( 1,- 1) GROUP BY a.awards_id HAVING COUNT ( a.awards_id ) >0" +
                    ")";
           RecordSet set = new RecordSet();
           set.executeSql(sql);
            while(set.next()){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id",set.getString("id"));                        	// 奖项配置id
                jsonObject.put("name",set.getString("name"));                 		// 奖项配置名称
                jsonObject.put("startTime",set.getString("start_time"));        	// 开始时间
                jsonObject.put("endTime",set.getString("end_time"));            	// 结束时间
                jsonObject.put("createTime",set.getString("create_time"));            	// 结束时间
                jsonObject.put("dateType",set.getString("date_type"));          	// 时间类型：1 年度 2 季度
                jsonObject.put("hasAbout", set.getString("about")==null||set.getString("about").equals("")?false:true);// 是否有描述
                String start=   set.getString("start_time");
                String end=   set.getString("end_time");
                String createTime=   set.getString("create_time");
                long  endTime = null == end?((23*60+59)*60+59)*1000:((23*60+59)*60+59)*1000+sdf.parse(end).getTime();
                long  startTime = null == start?sdf.parse(createTime).getTime():sdf.parse(start).getTime();
                if (date.getTime()>endTime){                                // 当前状态：0 未开始 1 进行中 -1 已结束
                    jsonObject.put("status",-1);
                }else if (date.getTime()>startTime){
                    jsonObject.put("status",0);
                }else {
                    jsonObject.put("status",1);
                }
                array.add(jsonObject);
            }
        }
        retObject.put("data", array);
    } catch (Exception e) {
        e.printStackTrace();
        retObject.put("code", -1);
        retObject.put("msg", e.getMessage());
    } finally {
        PrintWriter printWriter = response.getWriter();
        printWriter.append(retObject.toJSONString());
        if (printWriter != null)  printWriter.close();
    }
%>
 